from rest_framework.viewsets import GenericViewSet
from django_filters import FilterSet,filters
from django_filters.rest_framework import DjangoFilterBackend


from api import models
from api.serializers.topic import TopicSerializer
from api.extension.filter import SelfFilterBackend
from api.extension.mixins import DigCreateModelMixin,DigListModelMixin,DigUpdateModelMixin,DigDestroyModelMixin


class TopicFilterSet(FilterSet):
    latest_id = filters.NumberFilter(field_name="id",lookup_expr="lt")

    class Meta:
        models = models.Topic
        fields = ["latest_id",]


class TopicView(DigCreateModelMixin, DigListModelMixin, DigUpdateModelMixin, DigDestroyModelMixin, GenericViewSet):
    """主题"""

    # 当前登录用户的调整
    filter_backends = [SelfFilterBackend, DjangoFilterBackend]
    filterset_class = TopicFilterSet

    queryset = models.Topic.objects.filter(deleted=False).order_by("-id")
    serializer_class = TopicSerializer

    def perform_create(self, serializer):
        serializer.save(user=self.request.user)

    def perform_destroy(self, instance):
        instance.deleted = True
        instance.save()
